import { _decorator, Component, Animation, sys, Node, Label, Sprite, SpriteFrame, Prefab, instantiate, Button } from 'cc';
import { Config } from './Config';
import { SDKManager } from './SDKManager';
const { ccclass, property } = _decorator;

@ccclass('Item')
export class Item {
    @property
    title = '标题';
    @property
    content = '提示内容';
    @property(SpriteFrame)
    img: SpriteFrame | null = null;
}

@ccclass('msgShare')
export class msgShare extends Component {


    @property(Animation)
    public anim: Animation = null!;

    @property(Node)
    public mask: Node = null!;

    @property({ type: Label })
    public title: Label = null!;

    @property({ type: Sprite })
    public img: Sprite = null!;

    @property({ type: Label })
    public content: Label = null!;

    @property({ type: Sprite })
    public icon: Sprite = null!;

    @property([Item])
    items: Item[] = [];

    @property({ type: SpriteFrame })
    public icons: SpriteFrame[] = [];

    @property(Button)
    public btn: Button = null!

    public idx: number = 0
    public mode: number = 0

    start() {
        this.mask.setScale(1, Config.heightScaling)

    }
    /**
     * 
     * @param i 第几个道具
     * @param mode 分享  0||视频  1
     */
    init(i: number, mode: number = 0) {
        this.idx = i || 0
        this.mode = mode || 0
        this.title.string = this.items[i].title
        this.content.string = this.items[i].content
        this.img.spriteFrame = this.items[i].img
        this.icon.spriteFrame = this.icons[mode]
        //传递
        this.btn.clickEvents[0].customEventData = JSON.stringify({ mode: this.mode, idx: this.idx })

    }

    show() {
        //console.log('clickEvents', this.btn.clickEvents[0].customEventData)
        this.node.active = true
        this.anim?.play('pop-up');
        SDKManager.showCustomAd('bottom');
    }

    hide() {
        this.anim?.play('pop-out');
        this.anim.on(Animation.EventType.STOP, this.onAnimationStop, this);
        SDKManager.hideCustomAd();
    }


    onAnimationStop() {
        this.node.active = false;
        this.anim.off(Animation.EventType.STOP, this.onAnimationStop, this);
    }
}


